package com.kymatrix.kycarbon.xsmd.kbpm.server.core.engine.core;

import cn.hutool.core.lang.Dict;
import com.kymatrix.kycarbon.xsmd.kbpm.server.core.engine.FlowEngine;
import com.kymatrix.kycarbon.xsmd.kbpm.server.model.enums.ProcessTaskStateEnum;
import com.kymatrix.kycarbon.xsmd.kbpm.server.core.engine.model.NodeModel;
import com.kymatrix.kycarbon.xsmd.kbpm.server.core.engine.model.ProcessModel;
import com.kymatrix.kycarbon.xsmd.kbpm.server.model.entity.ProcessInstanceEntity;
import com.kymatrix.kycarbon.xsmd.kbpm.server.model.entity.ProcessTaskEntity;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * 执行对象参数
 * @author mldong
 * @date 2023/4/25
 */
@Data
public class Execution {
    // 流程实例ID
    private Long processInstanceId;
    // 当前流程任务ID
    private Long processTaskId;
    // 执行对象扩展参数
    private Dict args;
    // 当前流程模型
    private ProcessModel processModel;
    // 当前任务
    private ProcessTaskEntity processTaskEntity;
    // 当前流程实例
    private ProcessInstanceEntity processInstanceEntity;
    // 所有任务集合
    private List<ProcessTaskEntity> processTaskEntityList = new ArrayList<>();
    // 是否可合并
    private boolean isMerged;
    // 流程引擎对象
    private FlowEngine engine;
    // 操作人
    private String operator;
    // 当前节点模型
    private NodeModel nodeModel;
    /**
     * 添加任务到任务集合
     * @param processTaskEntity
     */
    public void addTask(ProcessTaskEntity processTaskEntity) {
        this.processTaskEntityList.add(processTaskEntity);
    }

    /**
     * 添加任务集合
     * @param processTaskEntities
     */
    public void addTasks(List<ProcessTaskEntity> processTaskEntities) {
        this.processTaskEntityList.addAll(processTaskEntities);
    }
    /**
     * 获取正在进行中的任务列表
     * @return
     */
    public List<ProcessTaskEntity> getDoingTaskList() {
        return this.processTaskEntityList.stream().filter(item->{
            return ProcessTaskStateEnum.DOING.getCode().equals(item.getTaskState());
        }).collect(Collectors.toList());
    }
 }
